﻿using System;

namespace TestFileSystem.Models
{
    /// <summary>
    /// Реализация паттерна UnitOfWork
    /// </summary>
    public class UnitOfWork : IDisposable
    {
        private bool _disposed = false;
        private readonly TestFileSystemEntities _dataContext = new TestFileSystemEntities();

        public TestFileSystemEntities DataContext
        {
            get { return _dataContext; }
        }

        /// <summary>
        /// Сохраняем изменения
        /// </summary>
        public void Commit()
        {
            _dataContext.SaveChanges();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if(!_disposed)
            {
                if (disposing)
                {
                    _dataContext.Dispose();
                }
            }

            _disposed = true;
        }

        ~UnitOfWork()
        {
            Dispose(false);
        }
    }
}